package cmu.andersonlab.hardpropagate;

import java.awt.*;
import java.awt.font.FontRenderContext;
import java.awt.geom.*;
import javax.swing.*;


public class tile {
    public int x, y, w, h;
    public Color background;
    public String value;
    public String answer;

    public static Font font = new Font("SansSerif", Font.PLAIN, 46);
    //final static font = ;


    public tile (String value, String answer, Color background) {
        this.value = value;
        this.answer = answer;
        this.background = background;
        this.w = 50;
        this.h = 50;
    }

    public void space (Graphics2D gr) {
        FontRenderContext frc = gr.getFontRenderContext();
        Rectangle2D bounds = font.getStringBounds(value, frc);

        w = 50;
        if (bounds.getWidth() > w) {
            w = (int)bounds.getWidth();
        }
        h = 50;
    }

    public void draw (Graphics2D g) {
        // FontMetrics metrics = g.getFontMetrics(font);
        // int adv = metrics.stringWidth(text);
        // g.setFont(new Font("Serif", Font.PLAIN, 28));
        // g.setColor(
        // g.drawString
        g.setColor(background);
        g.fillRect(x,y,w,h);
        g.setColor(new Color(200,200,200));
        g.drawRect(x,y,w,h);
        g.setColor(Color.black);

        g.setFont(font);
        FontRenderContext frc = g.getFontRenderContext();
        FontMetrics metrics = g.getFontMetrics(font);
        Rectangle2D bounds = font.getStringBounds(value, frc);

        //System.out.println(String.format("%f %f %f %f %d %d", bounds.getX(), bounds.getY(), bounds.getWidth(), bounds.getHeight(), metrics.getAscent(), metrics.getDescent()));

        g.drawString(value,(int)(x+(w-bounds.getWidth())/2), (int)(y+h/2-bounds.getHeight()/2+metrics.getAscent()));
    }

    public int right () {
        return this.x + this.w;
    }

    public int bottom () {
        return this.y + this.h;
    }

    public static tile gray_tile (String value) {
        return new tile (value, value, Color.white);
    }
    public static tile blue_tile (String answer) {
        return new tile ("", answer, new Color (173, 216, 230));
    }

}
